/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import DTO.xmlobject.Districts.District;
import DTO.xmlobject.Districts.Districts;


/**
 *
 * @author ASUS
 */
public class DistrictDAO extends Dataconnection{
    private District ReadDistrictFromDS(ResultSet rs) {
        District result = new District();
        if (rs != null) {
            try {
                result.setDistrictID(rs.getLong("DistrictID"));
                result.setDistrictName(rs.getString("DistrictName"));
                result.setDescription(rs.getString("Description"));
                result.setIsDelete(rs.getBoolean("IsDelete"));
                result.setCreatedDate(rs.getDate("CreatedDate"));
                result.setCreatedBy(rs.getString("CreatedBy"));
                result.setUpdatedDate(rs.getDate("UpdatedDate"));
                result.setUpdatedBy(rs.getString("UpdatedBy"));
            } catch (SQLException ex) {
                Logger.getLogger(DistrictDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return result;
    }

    public District getDistrictById(int id) {
        try {
            proc_stmt = DAOConnection.prepareCall("{ call spDistrictGet(?) }");
            proc_stmt.setLong("DistrictID", id);
            rs = proc_stmt.executeQuery();
            if (rs.next()) {
                return ReadDistrictFromDS(rs);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DistrictDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public Districts getAllDistrict() {
        Districts result = new Districts();
        try {
            proc_stmt = DAOConnection.prepareCall("{ call spDistrictGetList() }");
            rs = proc_stmt.executeQuery();
            while (rs.next()) {
                result.getDistrict().add(ReadDistrictFromDS(rs));
            }
        } catch (SQLException ex) {
            Logger.getLogger(DistrictDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }
    
    public Districts Search(String name) {
        Districts result = new Districts();
        try {
            proc_stmt = DAOConnection.prepareCall("{ call spdistrictSearch(?) }");
            proc_stmt.setString("DistrictName", name);
            rs = proc_stmt.executeQuery();
            while (rs.next()) {
                result.getDistrict().add(ReadDistrictFromDS(rs));
            }
        } catch (SQLException ex) {
            Logger.getLogger(DistrictDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }

}
